﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace Library
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string lstrFile = openFileDialog1.FileName;
            //Bitmap myBitmap = new Bitmap(lstrFile);
            //// 拷贝位图到剪贴板
            //Clipboard.SetDataObject(myBitmap);
            //// 获取对象格式
            //DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
            //richTextBox1.Paste(myFormat);


           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap wie = new Bitmap(Image.FromFile(openFileDialog1.FileName), new Size(400,400));
                Clipboard.SetImage(wie);
                richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MemoryStream wie = new MemoryStream();
            //richTextBox1.SaveFile(wie, RichTextBoxStreamType.TextTextOleObjs);
            ////把数据存到数据库
            ////byte [] ww=new byte[1024];
            ////wie.Read(ww, 0, 1024);
            //BinaryWriter bina = new BinaryWriter(wie);
            SqlConnection con=new SqlConnection("data source=sbynegiw\\liwangxing;database=airways;user id=sa;password=sbynegiw");
            SqlCommand incom = new SqlCommand();
            //incom.CommandType = CommandType.StoredProcedure;
            incom.Connection = con;  
            incom.Parameters.Add("@contentData", SqlDbType.NText);
            incom.Parameters["@contentData"].Value = richTextBox1.Rtf;
            //byte[] aa = wie.GetBuffer();
            incom.CommandText = "insert into ArticleT(DirectoryID,ArticleContent) values(5,@contentData)";

            //SqlParameter ww = new SqlParameter("contentData", aa);
            //incom.Parameters.Add(ww);
            //incom.CommandText = "storeData";
           
            con.Open();
            if (incom.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("成功");
            }
            con.Close();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=sbynegiw\\liwangxing;database=airways;user id=sa;password=sbynegiw");
            SqlCommand duqu = new SqlCommand("select ArticleContent from ArticleT where ArticleID=2", con);
            con.Open();
            //MemoryStream mm = new MemoryStream();
            ////mm=(MemoryStream)duqu.ExecuteScalar();
            //byte[] content =(byte[]) duqu.ExecuteScalar();
            //foreach (byte aa in content)
            //{
            //    mm.WriteByte(aa); 
            //}
            //byte[] wewww = mm.GetBuffer();

            richTextBox1.Rtf= duqu.ExecuteScalar().ToString();
            
           // Image wee= Image.FromStream(mm, true, true);
            //Clipboard.SetImage(wie);
            //richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
            //mm.WriteByte();
            //mm.WriteTo();
            //BinaryReader bb = new BinaryReader((duqu.ExecuteScalar()));
            //BinaryReader ee=new BinaryReader(mm);
            //richTextBox1.Clear();
            //System.IO.StreamReader ie = new StreamReader(mm);


            //richTextBox1.LoadFile(mm, RichTextBoxStreamType.TextTextOleObjs);
            
            //richTextBox1.LoadFile(oo, RichTextBoxStreamType.TextTextOleObjs);
        }

        string contentaa = null;
        private void button4_Click(object sender, EventArgs e)
        {
            contentaa = richTextBox1.Rtf;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (contentaa != null)
            {
                richTextBox1.Rtf = contentaa;
            }
        }
    }
}